package com.xt.LibaryDeskManager.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xt.LibaryDeskManager.dao.SeatMapper;
import com.xt.LibaryDeskManager.dao.SelectSeatMapper;
import com.xt.LibaryDeskManager.dto.SeatDto;
import com.xt.LibaryDeskManager.dto.SelectSeatDto;
import com.xt.LibaryDeskManager.service.SeatService;
import com.xt.LibaryDeskManager.service.SelectSeatService;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.SelectSeatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 童归权
 * @version 1.0 2021/2/20
 */
@Service
public class SelectSeatServiceImpl implements SelectSeatService {
    @Autowired
   private SelectSeatMapper selectSeatMapper;
    @Autowired
    private SeatMapper seatMapper;

    @Override
    public Map getAllSelectSeat(Integer page, Integer limit,SelectSeatVo selectSeatVo) throws Exception {
        Page page1= PageHelper.startPage(page,limit,true);
        List<SelectSeatDto> list= selectSeatMapper.getAllSelectSeat(selectSeatVo);
        return StrUtils.creatMap(list,page1.getTotal());
    }

    @Override
    public SelectSeatDto getSelectSeatByseatNo(String seatNo) throws Exception {
        return selectSeatMapper.getSelectSeatByseatNo(seatNo);
    }

    @Override
    public List<SelectSeatDto> selectByTime(String begin1,String seatNo) throws Exception {
        return selectSeatMapper.selectByTime(begin1,seatNo);
    }

    @Override
    public int addSelectSeat(SelectSeatVo selectSeatVo) throws Exception {
        int i= selectSeatMapper.addSelectSeat(selectSeatVo);
        List<SelectSeatDto> list= selectSeatMapper.selectByTime(selectSeatVo.getBegin1(),selectSeatVo.getSeatNo());
        int b=0;
        if (list.size()!=0){
            for (SelectSeatDto s : list) {
                if (s.getStatue()!=3&&s.getStatue()!=4){
                    b+=s.getBegin2();
                }
            }
        }
        seatMapper.updateSeatStatue(selectSeatVo.getSeatNo(),b);
        return i;
    }

    @Override
    public int selectByTimeStudentNo(String studentNo,String begin1, Integer begin2) throws Exception {
       List<SelectSeatDto> list= selectSeatMapper.selectByTimeStudentNo(studentNo,begin1,begin2);
        int code=0;
       for (SelectSeatDto s : list) {
           System.out.println(s.getStatue());
             if (s.getStatue()!=3&&s.getStatue()!=4){
                     code=1;
                     break;
             }
        }
       return code;
    }

    @Override
    public Map selectSeatByStudentNo(Integer page,Integer limit, String studentNo) throws Exception {
        Page page1= PageHelper.startPage(page,limit,true);
        List<SelectSeatDto> list= selectSeatMapper.selectSeatByStudentNo(studentNo);
        return StrUtils.creatMap(list,page1.getTotal());
    }

    @Override
    public int updateSelectSeat(String seatNo, String selectId,int i,int j) throws Exception {
        SeatDto seatDto=seatMapper.getSeatBySeatNo(seatNo);
        seatMapper.updateSeatStatue(seatNo,seatDto.getStatue()-j);
        return selectSeatMapper.updateSelectSeat(selectId,i);
    }

    @Override
    public Map selectByTime(String begin1,String seatNo,Integer page,Integer limit) throws Exception {
        Page page1= PageHelper.startPage(page,limit,true);
        List<SelectSeatDto> list= selectSeatMapper.selectByTime(begin1, seatNo);
        return StrUtils.creatMap(list,page1.getTotal());
    }

    @Override
    public int updateSelectSeat(String begin1, Integer begin2, Integer statue1, Integer statue2)throws  Exception {
       return selectSeatMapper.updateSelectSeat1(begin1,begin2,statue1,statue2);
    }
}
